package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.GoodsEntity;
import com.qfedu.entity.GoodsTypeEntity;
import com.qfedu.entity.OrderEntity;
import com.qfedu.pojo.GoodsPojo;
import com.qfedu.service.WaresService;
import com.qfedu.util.Ajaxd;
import com.qfedu.util.uuidUtil;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

@Controller
public class WaresController {
    @Autowired
    private WaresService waresService;
//查找所有商品
    @RequestMapping("/queryAllWares.do")
    public String doqueryAllWares(HttpServletRequest request, GoodsPojo goodsPojo){
        PageHelper.startPage(goodsPojo.getPageNum(),goodsPojo.getPageSize());
        request.setAttribute("gname",goodsPojo.getGanme());
        List<GoodsEntity> goodsEntities = waresService.queryAllWares(goodsPojo);
        PageInfo<GoodsEntity> pageInfo = new PageInfo<>(goodsEntities);
        request.setAttribute("pageinfo",pageInfo);
        return "wares/wares-list";
    }

    //删除商品
    @RequestMapping("/deleteWaresById.do")
    @ResponseBody
    public Ajaxd dodeleteWares(String gidstr,HttpServletRequest request){
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag = waresService.deleteWaresById(gidstr);
        if (flag) {
            ajaxd.setFlag(true);
        } else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }


    //商品编辑跳转
    @RequestMapping("/waresedit.to")
    public String toWaresedit(GoodsPojo goodsPojo, HttpServletRequest request){
        List<GoodsEntity> goodsEntities = waresService.queryAllWares(goodsPojo);
        GoodsEntity goodsEntity=goodsEntities.get(0);

        List<GoodsTypeEntity> goodsTypeEntities = waresService.queryAllType(null);
        request.setAttribute("goodstypelist",goodsTypeEntities);
        request.setAttribute("goods",goodsEntity);
        return "wares/wares-edit";
    }

    @RequestMapping("/waresedit.do")
    @ResponseBody
    public Ajaxd doWaresedit(GoodsEntity goodsEntity, HttpServletRequest request, MultipartFile himage){
        Ajaxd ajaxd = new Ajaxd();
        //获取文件名称
        String filename = himage.getOriginalFilename();
        //
        String uuidstr= uuidUtil.getUUid();
        //最终文件名
        filename=uuidstr+filename;
        //获取相对路径
        String upload =request.getServletContext().getRealPath("images");
        File file = new File(upload);
        //判断当前文件是否存在
        if (!file.exists()){
            file.mkdirs();
        }
        String lastpath=upload+File.separator+filename;
        try {
            FileOutputStream fos = new FileOutputStream(lastpath);
            InputStream inputStream = himage.getInputStream();
            //文件复制
            FileCopyUtils.copy(inputStream,fos);
        }catch (Exception e){
            e.printStackTrace();
        }
        String imagepath="../manager/images/"+filename;
        goodsEntity.setGimage(imagepath);

        Boolean aBoolean = waresService.updateGoods(goodsEntity);
        if (aBoolean){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }



}
